package com.fort.teachermanagementsys.service.impl;

import cn.hutool.core.collection.CollUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fort.teachermanagementsys.entity.SysRole;
import com.fort.teachermanagementsys.entity.SysRoleMenu;
import com.fort.teachermanagementsys.mapper.SysRoleMapper;
import com.fort.teachermanagementsys.mapper.SysRoleMenuMapper;
import com.fort.teachermanagementsys.reponse.SysRoleResponse;
import com.fort.teachermanagementsys.request.PageParamRequest;
import com.fort.teachermanagementsys.request.SysRoleRequest;
import com.fort.teachermanagementsys.service.SysRoleService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Allen
 * @since 2024-04-11
 */
@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;



    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    // 查询所有角色
    public List<SysRole> getRoleList() {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", "0").orderByAsc("id");
        return sysRoleMapper.selectList(wrapper);
    }

    // 添加角色
    @Transactional
    public void addRole(SysRole role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        role.setDelFlag("0");
        sysRoleMapper.insert(role);
    }

    // 更新角色
    @Transactional
    public void updateRole(SysRole role) {
        role.setUpdateTime(LocalDateTime.now());
        sysRoleMapper.updateById(role);
    }

    // 删除角色
    @Transactional
    public void deleteRole(Integer id) {
        SysRole role = sysRoleMapper.selectById(id);
        if (role == null || !"0".equals(role.getDelFlag())) {
            throw new RuntimeException("角色不存在");
        }
        role.setDelFlag("2");
        role.setUpdateTime(LocalDateTime.now());
        sysRoleMapper.updateById(role);
        // 删除关联的菜单权限
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", id);
        roleMenuMapper.delete(wrapper);
    }

    // 根据 ID 查询角色
    public SysRole getRoleById(Integer id) {
        return sysRoleMapper.selectById(id);
    }

    // 分配菜单权限
    @Transactional
    public void assignMenus(Integer roleId, List<Integer> menuIds) {
        // 删除旧的菜单关联
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        roleMenuMapper.delete(wrapper);
        // 插入新的菜单关联
        for (Integer menuId : menuIds) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        }
    }

    // 查询角色关联的菜单 ID
    public List<Integer> getMenuIdsByRoleId(Integer roleId) {
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        return roleMenuMapper.selectList(wrapper).stream()
                .map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
    }
}
